package com.csdj.t309springboot.controller;
import com.csdj.t309springboot.common.WebResult;
import com.csdj.t309springboot.entity.Topic;
import com.csdj.t309springboot.service.TopicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/topic")
public class TopicController {
    private Logger log = LoggerFactory.getLogger("test");

    @Resource
    private TopicService topicService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("index")
    public String index(){
        return "topic";
    }

    @ResponseBody
    @PostMapping("/topiclist")
    public List<Topic> topicList(){
        List<Topic> topicList=topicService.topiclist();
        return topicList ;
    }


    //当这个num为时点赞成功,为0时不点赞
    private String num=null;
    //点赞总数有多少
    @ResponseBody
    @GetMapping("/detailsDianzan")
    public WebResult getDianZan(@RequestParam(value = "id",required = false)Integer id){
//        List<User> list = new ArrayList<>();
//        User user;
//        user=userservice.login("account","123456");
//        list.add(user);
//        WebResult webResult = new WebResult();
//        webResult.putData("user",user).putData("dum",sum);
        WebResult webResult = new WebResult();
        num=stringRedisTemplate.opsForValue().get(Integer.toString(id)+stringRedisTemplate.opsForValue().get("detailsUserid"));
        if(num!=null){
            if("0".equals(num)){
                stringRedisTemplate.opsForValue().set(Integer.toString(id)+stringRedisTemplate.opsForValue().get("detailsUserid"),"1");
                num = "1";
                stringRedisTemplate.opsForValue().increment("dianzanshu"+id,1);
            }else {
                stringRedisTemplate.opsForValue().set(Integer.toString(id)+stringRedisTemplate.opsForValue().get("detailsUserid"),"0");
                num = "0";
                stringRedisTemplate.opsForValue().increment("dianzanshu"+id,-1);
            }
        }else {
            stringRedisTemplate.opsForValue().set(Integer.toString(id) + stringRedisTemplate.opsForValue().get("detailsUserid"), "1");
            num = "1";
            stringRedisTemplate.opsForValue().increment("dianzanshu"+id,1);
        }
        webResult.putData("num",num).putData("sum",stringRedisTemplate.opsForValue().get("dianzanshu"+id));
        return webResult;
    }
    //查询点赞数
    @ResponseBody
    @GetMapping("/dd")
    public String dd(@RequestParam(value = "id",required = false)Integer id){
        if(stringRedisTemplate.opsForValue().get("dianzanshu"+id)!=null){
            return stringRedisTemplate.opsForValue().get("dianzanshu"+id);
        }
        return "0";
    }


    @ResponseBody
    @GetMapping("datailsindex")
    public List<Topic> detailsindex(@RequestParam(value = "id",required = false) Integer id){
        List<Topic> list=topicService.topicAllList(id);

        return list;
    }

    @PostMapping("publish")
    public String publishTopic(String title, @RequestParam("description") String content,String tag,
            String topicUser, Model model){
        log.info("title="+title+",content="+content+",tag="+tag+",topicUser="+topicUser);
        Topic topic = new Topic();
        topic.setTitle(title);
        topic.setContent(content);
        topic.setTopicTag(tag);
        topic.setTopicUser(topicUser);
        int insert = topicService.insert(topic);
        if(insert >0){
            log.info("成功");
            //重定向到 /backend
            return "redirect:/topic/index";
        }else{
            log.info("失败");
            model.addAttribute("error","发布失败");
            return "error";
        }


    }


}
